package api

import (
	"git.supremind.info/product/box/vehicle/internal/dao"
	"git.supremind.info/product/box/vehicle/internal/dao/req"
	"github.com/gin-gonic/gin"
	"net/http"
)

func GetUpgradeVersions(c *gin.Context) {
	list := dao.GetUpgradeDebList()
	c.JSON(http.StatusOK, gin.H{"code": 0, "message": "请求成功", "data": list})
}

func SaveUpgradeDeb(c *gin.Context) {
	var request req.SaveUpgradeDebReq
	if err := c.Bind(&request); err != nil {
		c.JSON(http.StatusOK, gin.H{"code": -1, "message": err.Error()})
		return
	}
	err := dao.SaveUpgradeDeb(request)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"code": -1, "message": err.Error()})
	} else {
		c.JSON(http.StatusOK, gin.H{"code": 0, "message": "请求成功"})
	}
}

func DeleteUpgradeVersion(c *gin.Context) {
	var request req.DelUpgradeDebReq
	if err := c.Bind(&request); err != nil {
		c.JSON(http.StatusOK, gin.H{"code": -1, "message": err.Error()})
		return
	}
	err := dao.DeleteUpgradeVersion(request)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"code": -1, "message": err.Error()})
	} else {
		c.JSON(http.StatusOK, gin.H{"code": 0, "message": "请求成功"})
	}
}

//整包升级
func DoUpgrade(c *gin.Context) {
	var request req.UpgradeBoxReq
	if err := c.Bind(&request); err != nil {
		c.JSON(http.StatusOK, gin.H{"code": -1, "message": err.Error()})
		return
	}
	err := dao.DoUpgrade(request)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"code": -1, "message": err.Error()})
	} else {
		c.JSON(http.StatusOK, gin.H{"code": 0, "message": "请求成功"})
	}
}

//SDK升级
func DoSdkUpgrade(c *gin.Context) {
	var request req.UpdateSdkVo
	if err := c.Bind(&request); err != nil {
		c.JSON(http.StatusOK, gin.H{"code": -1, "message": err.Error()})
		return
	}
	err := dao.DoSdkUpgrade(request)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"code": -1, "message": err.Error()})
	} else {
		c.JSON(http.StatusOK, gin.H{"code": 0, "message": "请求成功"})
	}
}
